package com.example.user.controller;


import annotation.RequireRoles;
import com.example.user.service.ProductServiceImpl;
import entity.PageParam;
import entity.Rsp;
import entity.Product;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 商品模块接口
 */

@RestController
@Api(value = "商品 模块接口",description="商品 模块接口")
public class ProductController {

    private static final Logger log = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    ProductServiceImpl productService;



    @PostMapping("/searchSomeProductByPage")
    @RequireRoles(roles = {"admin","super_admin"},index = 2)
    @ApiOperation("查询所有商品 支持 多条件 分页 排序 查询")
    public Rsp searchSomeByPage(@RequestBody PageParam<Product> productPageParam,
                                @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){

        log.info("userId:{}",userId);
        log.info("roleId:{}",roleSign);

        return Rsp.success(productService.searchSomeByPage(productPageParam));
    }


    @RequireRoles(roles = {"super_admin","admin"},index = 2)
    @PostMapping("/addOneProduct")
    @ApiOperation("添加单个商品")
    public Rsp addOne(@RequestBody @Valid Product product, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){
		
        product.setCreateUserId(userId);
        return productService.addOne(product);
    }


    @RequireRoles(roles = {"super_admin"},index = 2)
    @PostMapping("/deleteOneProductTrue")
    @ApiOperation("通过ID物理删除商品")
    public Rsp deleteOneProductTrue(@RequestBody Product product, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){

        return productService.deleteOneTrue(product.getId());
    }

    @RequireRoles(roles = {"super_admin","admin"},index = 2)
    @PostMapping("/updateProduct")
    @ApiOperation("动态修改商品")
    public Rsp updateProduct(@RequestBody Product product, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){


		product.setUpdateUserId(userId);
        return productService.update(product);
    }

    @RequireRoles(roles = {"super_admin"},index = 2)
    @PostMapping("/deleteProductLogical")
    @ApiOperation("通过ID逻辑删除商品")
    public Rsp deleteProductLogical(@RequestBody Product product, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){



        return productService.deleteLogical(product);
    }

    @RequireRoles(roles = {"super_admin"},index = 2)
    @PostMapping("/recoverProductLogical")
    @ApiOperation("逻辑恢复商品")
    public Rsp recoverProductLogical(@RequestBody Product product, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){



        return productService.recoverLogical(product);
    }


    @RequireRoles(roles = {"super_admin"},index = 2)
    @PostMapping("/deleteBatchProductTrue")
    @ApiOperation("批量物理删除商品")
    public Rsp deleteBatchProductTrue(@RequestBody List<String> ids, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){



        return productService.deleteBatchTrue(ids);
    }

    @RequireRoles(roles = {"super_admin"},index = 2)
    @PostMapping("/deleteBatchProductLogical")
    @ApiOperation("批量逻辑删除商品")
    public Rsp deleteBatchProductLogical(@RequestBody List<String> ids, @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){



        return productService.deleteBatchLogical(ids);
    }


}
